package com.example.petshopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.petshopping.R;
import com.example.petshopping.model.SpecialPerformanceItem;

import java.util.List;

/**
 * 专场列表适配器，绑定布局 item_special_performance_list
 */
public class SpecialPerformanceAdapter extends RecyclerView.Adapter<SpecialPerformanceAdapter.ViewHolder> {

    public interface OnItemClickListener {
        void onItemClick(SpecialPerformanceItem item, int position);
        void onDeleteClick(SpecialPerformanceItem item, int position);
    }

    private final Context context;
    private final List<SpecialPerformanceItem> dataList;
    private OnItemClickListener listener;

    public SpecialPerformanceAdapter(Context context, List<SpecialPerformanceItem> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_special_performance_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SpecialPerformanceItem item = dataList.get(position);

        holder.titleView.setText(item.getTitle());

        if (item.getTimeText() != null && !item.getTimeText().isEmpty()) {
            holder.timeView.setText(item.getTimeText());
            holder.timeView.setVisibility(View.VISIBLE);
        } else {
            holder.timeView.setVisibility(View.GONE);
        }

        setImage(holder.imageView, item.getImageUrl());

        holder.badgeView.setVisibility(item.isPromoted() ? View.VISIBLE : View.GONE);

        Integer count = item.getCount();
        holder.countView.setText(count != null ? String.valueOf(count) : "0");
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) listener.onItemClick(item, holder.getAdapterPosition());
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private void setImage(ImageView imageView, String imageUrl) {
        if (imageUrl == null || imageUrl.isEmpty()) {
            imageView.setImageResource(R.drawable.cat);
            return;
        }
        if (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) {
            Glide.with(context)
                    .load(imageUrl)
                    .apply(new RequestOptions()
                            .placeholder(R.drawable.yugang)
                            .error(R.drawable.yugang)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .centerCrop())
                    .into(imageView);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView badgeView;
        TextView titleView;
        TextView timeView;
        TextView countView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.special_performance_image);
            badgeView = itemView.findViewById(R.id.special_performance_badge);
            titleView = itemView.findViewById(R.id.special_performance_title);
            timeView = itemView.findViewById(R.id.special_performance_time);
            countView = itemView.findViewById(R.id.special_performance_count);
        }
    }
}


